﻿using System;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace HiStaff.Dal
{
    public sealed class DBConnection
    {
        private static string DEFAULT_FORMAT_CONNECTION = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3}))(CONNECT_DATA=(SID={4})));User Id={0};Password={1};";
        private static OracleConnection con = new OracleConnection();
        private static OracleTransaction txn;
        public static string ConnectionString { get; set; }

        private DBConnection()
        {
        }

        public static bool TestConnection()
        {
            try
            {
                GetConnection();
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteExceptionLog(ex, "DBConnection");
                return false;
            }
        }
        public static bool TestConnection(string user, string pass, string server, string port, string service)
        {
            OracleConnection orclCon = new OracleConnection();
            try
            {
                string constring = string.Format(DEFAULT_FORMAT_CONNECTION, user, pass, server, port, service);

                orclCon.ConnectionString = constring;
                orclCon.Open();

                return true;
            }
            catch (Exception ex)
            {
                Log.WriteExceptionLog(ex, "TestConnection");
                return false;
            }
            finally
            {
                orclCon.Close();
            }
        }
        public static bool TestConnection(string connectString)
        {
            OracleConnection orclCon = new OracleConnection();
            try
            {
                orclCon.ConnectionString = connectString;
                orclCon.Open();

                return true;
            }
            catch (Exception ex)
            {
                Log.WriteExceptionLog(ex, "TestConnection");
                return false;
            }
            finally
            {
                orclCon.Close();
            }
        }
        internal static OracleConnection GetConnection()
        {
            try
            {
                string ConnString = ConfigurationManager.ConnectionStrings["HiStaffConnectionString"].ConnectionString;
                string ConnStringEncrypt = ConfigurationManager.AppSettings["EncryptConnectionString"];

                if (con.State == ConnectionState.Closed ||
                    con.State == ConnectionState.Broken)
                {
                    if (!string.IsNullOrEmpty(ConnStringEncrypt) &&
                    ConnStringEncrypt.ToUpper() == "TRUE")
                    {
                        ConnString = CryptographyManager.Decrypt(ConnString);
                    }

                    con.ConnectionString = ConnString;
                    con.Open();
                }

                return con;
            }
            catch (Exception ex)
            {
                Log.WriteExceptionLog(ex, "GetConnection");
                throw ex;
            }
        }

        public static void Close()
        {
            try
            {
                con.Close();
            }
            catch
            {
                con = new OracleConnection();
            }
        }

        public static void BeginTransaction()
        {
            txn = GetConnection().BeginTransaction();
        }

        public static void Rollback()
        {
            txn.Rollback();
            txn.Dispose();
        }

        public static void Commit()
        {
            txn.Commit();
            txn.Dispose();
        }
    }
}
